package com.gs.common.util;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gs.entity.GeneralPage;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;

public class SQLUtils {


    /**
     * 批量删除Sql
     * @param tableName 表名
     * @param ids  目标Id列表
     * @return
     */
    public static String deleteBatchSql(String tableName, Long[] ids) {
        return new SQL() {{
            DELETE_FROM(tableName);
            if (ids.length > 1) {
                for (int i = 0; i < ids.length; i++) {
                    WHERE("id = " + ids[i]);
                    if (i != ids.length - 1) {
                        OR();
                    }
                }
            }

        }}.toString();
    }


    /**
     * 分页sql
     * @param baseMapper
     * @param wrapper
     * @param page
     * @param limit
     * @param <T>
     * @return
     */
    public static <T> GeneralPage<T> toPageSql(BaseMapper<T> baseMapper, QueryWrapper<T> wrapper, Long page, Long limit) {
        Page page2 = new Page(page, limit);

        //MP自带的selectPage方法，有兴趣的同学，可以看一下源码

        Page iPage = baseMapper.selectPage(page2, wrapper);

      
        GeneralPage<T> generalPage = new GeneralPage<>();
        List<T> records = iPage.getRecords();
        Integer current = Convert.toInt(iPage.getCurrent()); // 当前页
        Integer total = Convert.toInt(iPage.getTotal()); // 总数据量
        Integer pageCount = Convert.toInt(iPage.getPages()); // 多少页
        generalPage.setList(records);
        generalPage.setPageCount(pageCount);
        generalPage.setTotal(total);
        generalPage.setPage(current);

        return generalPage;
    }

    /**
     * 分页sql
     * @param baseMapper
     * @param wrapper
     * @param page
     * @param limit
     * @param <T>
     * @return
     */
    public static <T> GeneralPage<T> toPageSql(BaseMapper<T> baseMapper, QueryWrapper<T> wrapper, Object page, Object limit) {
        Long pageL = Convert.toLong(page);
        Long limitL = Convert.toLong(limit);
        return toPageSql(baseMapper, wrapper, pageL, limitL);
    }
        /**
         * 增加分页Sql
         *
         * @param sql 列表SQl
         * @param params  参数集
         * @return
         */
    public static String toPageSql(String sql, Map<String, Object> params) {

        Object page = params.get("page");
        Object limit = params.get("limit");

        if (ObjectUtil.isAllNotEmpty(page, limit) || limit != null) {
            if (page == null) page = 1;

            int p = Integer.parseInt(page.toString());
            int l = Integer.parseInt(limit.toString());
            if (p != 0 && l != 0) {
                return toPageSql(sql, p, l);
            }
        }
        return sql;
    }

    /**
     * 增加分页Sql
     *
     * @param sql 列表SQl
     * @param pageNo    页码
     * @param limit   数量
     * @return
     */
    public static String toPageSql(String sql, int pageNo, int limit) {
        if (pageNo != 0 && limit != 0) {
            sql += " limit " + (pageNo * limit - limit) + " ," + limit;
        }
        return sql;
    }

    /**
     * 增加分页Sql
     *
     * @param sql 列表SQl
     * @param pageNo    页码
     * @param limit   数量
     * @return
     */
    public static String toPageSql(String sql, Object pageNo, Object limit) {

        if (pageNo != null && limit != null) {
            Integer pageNoIntValue = Integer.parseInt(pageNo.toString());
            Integer limitIntValue = Integer.parseInt(limit.toString());
            sql += " limit " + (pageNoIntValue * limitIntValue - limitIntValue) + " ," + limitIntValue;
        }
        return sql;
    }
    /**
     * 列表SQL 转换为记数SQl
     *
     * @param sql 列表SQl
     * @return 转换后的SQL
     */
    public static String toCountSql(String sql) {
        sql = sql.toLowerCase();
        int fromIndex = sql.indexOf("from");
        if (fromIndex == -1) {
            return sql;
        }
        return "select count(*) " + sql.substring(fromIndex);

    }


}
